package com.rdiot.yx485.base

import androidx.lifecycle.MutableLiveData
import com.drake.serialize.serialize.serialLazy
import com.drake.serialize.serialize.serialLiveData
import com.rdiot.yx485.BuildConfig
import com.rdiot.yx485.bean.AppVersionData
import com.rdiot.yx485.bean.DeviceTypeData
import com.rdiot.yx485.bean.FamilyData
import com.rdiot.yx485.bean.LoginData
import com.rdiot.yx485.bean.UserData
import com.rdiot.yx485.bean.WeatherData
import com.rdiot.yx485.bean.WelcomeData
import com.rdiot.yx485.bean.YXBean
import com.rdiot.yx485.net.Api
import com.rdiot.yx485.ui.login.model.LoginRecord

/**
 * 本地数据
 * @author mR2hao
 * @date 2022/11/17
 */
object LocalData {
    /** Api地址 */
    var apiHost: ApiHost by serialLazy(if (!BuildConfig.DEBUG) ApiHost.TEST_HOST else ApiHost.PRODUCE_HOST)

    /** 验证码倒计时剩余 */
    var lastGetCodeTime: Long by serialLazy(0)

    /** 登录数据 */
    var loginData: LoginData? by serialLazy()

    /** 用户数据 */
    val userData by serialLiveData(UserData())

    /** 用户家庭数据 selFamilyId为空时，获取默认家庭 */
    val familyData by serialLiveData(FamilyData())

    /** 天气数据 selFamilyId为空时，获取默认家庭*/
    val weatherData by serialLiveData(WeatherData())

    /** 欢迎页数据*/
    val welcomeData by serialLiveData(WelcomeData())

    /** 所有家庭简单信息 */
    val allFamily by serialLiveData(mutableListOf<FamilyData>())

    /** 用户选择显示的家ID */
    var selFamilyId: MutableLiveData<String?> = MutableLiveData(null)

    /** 最后刷新时间 */
    var lastRefreshTime: Long = 0

    /** 最后一次登录类型 */
    var lastLoginRecord: LoginRecord? by serialLazy()

    /** 第一次安装app */
    var firstTimeInstallApp: Boolean by serialLazy(true)

    /** 移动端请求参数 */
    val yxBean: YXBean = YXBean()

    /** app版本信息 */
    val appVersionData: MutableLiveData<AppVersionData?> = MutableLiveData(null)

    /** 本日提醒更新APP用户（不判断年月，只判断日期，日期非本日则弹窗提醒） */
    var noReminderToday: Int by serialLazy(0)

    /** 测试 让Token失效 */
    var makeTokenErr by serialLazy(false)

    /** 设备类型 */
    val deviceTypeData by serialLiveData(mutableListOf<DeviceTypeData>())

    var firstBle: Int = 1

    var firstCamera: Boolean by serialLazy(true)

    /** 登出 */
    fun logout() {
        lastGetCodeTime = 0
        loginData = null
        userData.value = null
        familyData.value = null
        weatherData.value = null
        selFamilyId.value = null
        allFamily.value = null
    }
}

enum class ApiHost(
    val hostName: String,
    val host: String,
    val avatarHost: String
) {
    /** 生产环境 */
    PRODUCE_HOST("生产环境", Api.HOST, Api.AVATAR_HOST),

    /** 测试环境 */
    TEST_HOST("测试环境", Api.TEST_HOST, Api.TEST_AVATAR_HOST)
  //  TEST_HOST("测试环境", Api.TEST_HOST, Api.TEST_AVATAR_HOST)
}